<?php
/**
 * 班级
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/1
 * Time: 9:46
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Classroom extends Base
{
    /**
     * 班级首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $where['class_del'] = 1;
            $count = Db::name('class')->where($where)->count();// 获取总条数
            $lists = Db::name('class')
                ->field('class_id, class_name, class_status, class_time, class_del, class_update_time,
                class_sort')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('class_sort')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加班级
     * @return mixed|\think\response\Json
     */
    public function classAdd(){
        if(request()->isPost()){
            $class_name = trim(input('class_name'), ' ');
            //判断在冻结期间有没有添加相同名称的年级，有则不给用
            $class = Db::table('up_class')
                ->where('class_del', 1)
                ->where('class_status', 1)
                ->where('class_name', $class_name)
                ->find();
            if($class){
                return json(['code' => 100, 'msg' => '已有相同名称的班级，不可添加']);
            }
            
            $data = [
                'class_name' => $class_name,
                'class_time' => date('Y-m-d H:i:s'),
                'class_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_class')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('classadd');
    }
    
    /**
     * 编辑班级
     * @return mixed|\think\response\Json
     */
    public function classEdit(){
        if(Request::instance()->isPost()){
            $class_id = input('class_id');
            $class_name = trim(input('class_name'), ' ');
            if(empty($class_name)){
                return json(['code' => 100, 'msg' => '班级名称不能为空']);
            }
            //判断在冻结期间有没有添加相同名称的年级，有则不给用
            $class = Db::table('up_class')
                ->where('class_del', 1)
                ->where('class_status', 1)
                ->where('class_name', $class_name)
                ->find();
            if($class){
                return json(['code' => 100, 'msg' => '已有相同名称的班级，不可编辑']);
            }
            $data = [
                'class_name' => $class_name,
                'class_update_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_class')->where('class_id', $class_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $class_id = input('id');
        $class_name = Db::table('up_class')->where('class_id', $class_id)->value('class_name');
        $this->assign([
            'class_name' => $class_name,
            'class_id' => $class_id
        ]);
        return $this->fetch('classroom/classedit');
    }
    
    /**
     * 删除班级
     * @return \think\response\Json
     */
    public function classDel(){
        $class_id = input('id');
        $is_have = Db::table('up_grade_class')->where('gs_class_id', $class_id)->find();
        if(!empty($is_have)){
            return json(['code' => 100, 'msg' => '请先移除改班级下的师生']);
        }
        $res = Db::table('up_class')->where('class_id', $class_id)->setField('class_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 状态修改
     * @return \think\response\Json
     */
    public function classStatus(){
        $class_id = input('id');
        $class_status = input('num');
        //判断在冻结期间有没有添加相同名称的年级，有则不给用
        $class_name = Db::table('up_class')->where('class_id', $class_id)->value('class_name');
        $class = Db::table('up_class')
            ->where('class_del', 1)
            ->where('class_status', 1)
            ->where('class_id', 'neq', $class_id)
            ->where('class_name', $class_name)
            ->find();
        if($class){
            return json(['code' => 100, 'msg' => '已有相同名称的班级，不可启用']);
        }
        $res = Db::table('up_class')->where('class_id', $class_id)->setField('class_status', $class_status);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        if(request()->isPost ()){
            $id = input('id');
            $param = [
                'class_sort'=>input('value'),
            ];
            $res = Db::name('class')->where('class_id',$id)->update($param);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' =>' 修改失败']);
            }
        }
    }
}